package com.bawei.mall.user.factory;

import com.bawei.common.core.domain.R;
import com.bawei.common.core.web.page.TableDataInfo;
import com.bawei.mall.user.api.RemoteCusUserService;
import com.bawei.mall.user.domain.CusUser;
import com.bawei.mall.user.domain.MallUserCollectInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户服务降级处理
 *
 * @author bawei
 */
@Component
public class RemoteCusUserFallbackFactory implements FallbackFactory<RemoteCusUserService>
{
    private static final Logger log = LoggerFactory.getLogger(RemoteCusUserFallbackFactory.class);

    @Override
    public RemoteCusUserService create(Throwable throwable)
    {
        log.error("用户服务调用失败:{}", throwable.getMessage());
        return new RemoteCusUserService()
        {
            @Override
            public R<CusUser> getUserInfo(String username, String source) {
                return R.fail("获取用户失败:" + throwable.getMessage());
            }

            @Override
            public TableDataInfo selectCollectInfos(MallUserCollectInfo collect, String source) {
                throw new RuntimeException("获取商品收藏信息失败:" + throwable.getMessage());
            }

            @Override
            public R<List<MallUserCollectInfo>> getAllCollectInfos() {
                throw  new RuntimeException("获取商品收藏信息失败:" + throwable.getMessage());
            }
        };
    }
}
